package com.bxd.baodan.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.qcloud.timchat.model.CustomItem;
import com.tencent.qcloud.timchat.model.CustomMessage;
import com.tencent.qcloud.timchat.ui.ChatActivity;


public class StarDialog extends Dialog implements View.OnClickListener {

    protected DisplayImageOptions options;
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    boolean canClick = true;

    TextView tv1, tv2, tv3, tv4, tv5, tv6;
    EditText judge;


    public StarDialog(final Context context, int theme) {

        super(context, theme);
        this.setContentView(R.layout.im_star_dialog_layout_);
        this.getWindow().getAttributes().gravity = Gravity.CENTER;
    }


    public StarDialog(final Context context, final CustomItem item, final CustomMessage message) {


        this(context, R.style.CustomProgressDialog);

        final ImageView close = (ImageView) this.findViewById(R.id.close);
        final StarBarView sbv_starbar = (StarBarView) this.findViewById(R.id.sbv_starbar);
        final Button submit = (Button) this.findViewById(R.id.submit);


        tv1 = (TextView) this.findViewById(R.id.tv1);
        tv2 = (TextView) this.findViewById(R.id.tv2);

        tv3 = (TextView) this.findViewById(R.id.tv3);

        tv4 = (TextView) this.findViewById(R.id.tv4);

        tv5 = (TextView) this.findViewById(R.id.tv5);

        tv6 = (TextView) this.findViewById(R.id.tv6);


        judge = (EditText) findViewById(R.id.judge_et);

        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        tv5.setOnClickListener(this);
        tv6.setOnClickListener(this);


        if (item.getCustomIMType().equals("evaluate")) {

            submit.setText("已评分");
            submit.setFocusable(false);
            submit.setBackground(context.getResources().getDrawable(R.drawable.gray_slide));
            int score = Integer.parseInt(item.getScore());
            sbv_starbar.setIsIndicator(true);
            sbv_starbar.setStarRating(score);
        } else {
            submit.setFocusable(true);
            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2017/3/22  提交数据到服务器  回调成功 修改消息状态

                    final int score = (int) sbv_starbar.getStarRating();

                    if (score != 0 && canClick) {

                        NetUtil netUtil = new NetUtil((ChatActivity) context, Constant.APP_EVALUATE);
                        netUtil.setParams("score", score);
                        netUtil.setParams("inviteId", item.getInviteId());
                        netUtil.setParams("agentId", item.getAgentId());
                        netUtil.setParams("content", judge.getText().toString().trim());
                        netUtil.postRequest("", new NetUtil.RequestStringListener() {

                            @Override
                            public void onComplete(String result) {
                                canClick = false;
                                if (JSONUtil.isSuccess(result)) {
                                    message.getMessage().setCustomInt(1);
                                    LoginInfo info = LoginInfo.getInstance(context);
                                    JsonObject object = new JsonObject();
                                    //	{"CustomIMType":evaluate,"score":"5"}
                                    object.addProperty("CustomIMType", "evaluate");
                                    object.addProperty("score", score);
                                    object.addProperty("inviteId", item.getInviteId());
                                    object.addProperty("tel", info.getUser().getPhone());
                                    object.addProperty("name", info.getUser().getU_nick());
                                    object.addProperty("avatar", info.getUser().getUe_avatar());
                                    object.addProperty("other_name", item.name);
                                    object.addProperty("other_avatar", item.getAvatar());
                                    String json = object.toString();
                                    ((ChatActivity) context).sendCustomMessage(json);
                                    ((ChatActivity) context).adapter.notifyDataSetChanged();
                                    StarDialog.this.dismiss();
                                } else {
                                    StarDialog.this.dismiss();
                                }
                            }
                        });
                    }


                }
            });
        }


        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StarDialog.this.dismiss();
            }
        });


    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        if (!hasFocus) {
            dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        String str = "";

        switch (v.getId()) {
            case R.id.tv1:
                str = tv1.getText().toString();
                break;
            case R.id.tv2:
                str = tv2.getText().toString();
                break;
            case R.id.tv3:
                str = tv3.getText().toString();
                break;
            case R.id.tv4:
                str = tv4.getText().toString();
                break;
            case R.id.tv5:
                str = tv5.getText().toString();
                break;
            case R.id.tv6:
                str = tv6.getText().toString();
                break;

        }
        judge.setText(str + "\n");

    }
}